/*
 * time.c
 *
 *  Created on: 10/08/2011
 *      Author: Misael
 */


#include <hidef.h> /* for EnableInterrupts macro */
#include "Headers/derivative.h" /* include peripheral declarations */
#include "Headers/define.h"
#include "Headers/time.h"


void delay_ms(unsigned short time){
	unsigned short i = 0;
	i = time;
	for( ;i > 0 ; --i){
		delay_us(1000);
		__RESET_WATCHDOG();
	}
	
}

void delay_us(unsigned short time){
	TPM2SC = 0x0C;  		// BusCLK as clock source and prescaler with 16
							// to achieve a 1mhz clk source for the TMP 
	TPM2MOD = time;
	while(!TPM2SC_TOF); 
	TPM2SC = 0;
}

void TMP_pwm(void){
	TPM1SC_TOIE  = 0;
	TPM1SC_CPWMS = 0;
	TPM1SC_CLKSA = 1;
	TPM1SC_CLKSB = 0;
	TPM1SC_PS0   = 0;
	TPM1SC_PS1   = 1;
	TPM1SC_PS2   = 0;
	TPM1CNTH     = 0X00;
	TPM1CNTL     = 0X00;
	TPM1MODH     = 0Xc3;
	TPM1MODL     = 0X44;
	TPM1C2SC_CH2F= 0;
	TPM1C2SC_CH2IE=0;
	TPM1C2SC_MS2B =1;
	TPM1C2SC_MS2A =0;
	TPM1C2SC_ELS2B=1;
	TPM1C2SC_ELS2A=0;
	TPM1C2VH     = 0X00;
	TPM1C2VL     = 0X02;

	TPM2SC_TOIE  = 0;
	TPM2SC_CPWMS = 0;
	TPM2SC_CLKSA = 1;
	TPM2SC_CLKSB = 0;
	TPM2SC_PS0   = 1;
	TPM2SC_PS1   = 1;
	TPM2SC_PS2   = 0;
	TPM2CNTH     = 0Xc3;
	TPM2CNTL     = 0X44;
	TPM2MODH     = 0Xc3;
	TPM2MODL     = 0X44;
	TPM2C2SC_CH2F= 0;
	TPM2C2SC_CH2IE=0;
	TPM2C2SC_MS2B =1;
	TPM2C2SC_MS2A =0;
	TPM2C2SC_ELS2B=1;
	TPM2C2SC_ELS2A=0;
	TPM2C2VH     = 0X30;
	TPM2C2VL     = 0Xd1;
}
